package com.xxxx.server;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: yeb
 * @description: utils研究
 * @author: Honors
 * @create: 2021-09-03 09:35
 */
public class test3 {

    @Test
    public void test() throws IOException {

        File outFile = new File("D:\\YebTestFile\\5.docx");

        FileOutputStream out = new FileOutputStream(outFile);

        XWPFDocument poiWorldUtils = new XWPFDocument();

        List<String> titleList = new ArrayList();
        titleList.add("公司试卷");
        titleList.add("理论试卷");
        titleList.add("实操试卷");

        List<String> list2 = new ArrayList<>();
        list2.add("序号");
        list2.add("学员姓名");
        list2.add("部门代号");
        for (int i = 0; i < titleList.size(); i++) {
            list2.add(titleList.get(i));
        }
        list2.add("是否合格");
        list2.add("备注");

        System.out.println(list2.size());

        XWPFTable xwpfTable = setCellTitle(poiWorldUtils, list2);

//        poiWorldUtils.sav
        poiWorldUtils.write(out);
        out.close();
    }

    /**
     * 设置 word 表格标题行
     *
     * @param poiWorldUtils 传入操作 poiWorld
     * @param list          标题名称集合
     * @return
     */
    public XWPFTable setCellTitle(XWPFDocument poiWorldUtils, List<String> list) {
        XWPFTable table = poiWorldUtils.createTable(1, list.size());
        int x = 0;
        for (int i = 0; i < list.size(); i++) {
            String title = list.get(i);
            // 设置表格
            table.getRow(0).getCell(x).setText(title);
            x = x + 1;
        }
        return table;
    }

}
